<template>
  <el-dialog
    :visible="showdialog"
    :title="showTitle "
    width="700px"
    @close="close"
  >
    <el-form ref="departForm" label-width="120px" :model="formData" :rules="rule">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" size="small" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" size="small" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" size="small" style="width:80%" placeholder="请选择负责人">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          size="small"
          type="textarea"
          style="width:80%"
          :autosize="{ minRows: 4 }"
          placeholder="1-100个字符"
        />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="changeDepart">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartmentuser, getDepartmentList, postDepartment, getDepartmentDetail, putDepartment } from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    // dialogVisable: {  //show-dialog是这个值
    //   type: Boolean,
    //   default: false
    // }

    showdialog: { // 学到现在命名规范啊  传递过来 就不要fu定义的值
    // 而是父传递来的值的名字
    // 比如show-dialog是复合命名规范的 那porps里应该用show-dialog  不是dialogVisable
    // 明白？   show-dialog这个本来就不对  命名规范 不能以数字开头 不能用中文
    // 不能用-  可以用_
    // 不要纠结 你这个两个错  第一命名规范   第二正向传值  子是用父传递过来的名字
    // OK？还有吗
    // 为什么 你这个编辑器  反而用小驼峰报错了
      type: Boolean,
      default: false
    },
    currentid: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [],
      formData: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      rule:
        {
          code: [
            { required: true, message: '部门编码不能为空', trigger: 'blur' },
            { min: 2, max: 10,
              message: '部门编码的长度为2-10个字符', trigger: 'blur' },
            {
              trigger: 'blur',
              validator: async(rule, value, callback) => {
                let result = await getDepartmentList()
                if (this.formData.id) {
                  result = result.filter(item => item.id !== this.formData.id)
                }
                if (result.some(item => item.code === value)) {
                  callback(new Error('部门中已经有该编码了'))
                } else {
                  callback()
                }
              }
            }
          ],
          introduce: [
            { required: true, message: '部门介绍不能为空', trigger: 'blur' }
          ],
          managerId: [{ required: true, message: '部门编码不能为空', trigger: 'blur' }],
          name: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' },
            { min: 2, max: 10,
              message: '部门名称的长度为2-10个字符', trigger: 'blur' },
            {
              trigger: 'blur',
              validator: async(rule, value, callback) => {
                let result = await getDepartmentList()
                if (this.formData.id) {
                  result = result.filter(item => item.id !== this.formData.id)
                }
                if (result.some(item => item.name === value)) {
                  callback(new Error('部门中已经有该名称了'))
                } else {
                  callback()
                }
              }
            }
          ]
        }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getDepartmentuser()
  },
  methods: {
    async getDepartmentDetail() {
      this.formData = await getDepartmentDetail(this.currentid)
    },
    changeDepart() {
      this.$refs.departForm.validate(async(isOk) => {
        if (isOk) {
          if (this.formData.id) { // 这个if else用的有点low哈
            // 编辑模式
            // this.formData.pid=this.currentid
            await putDepartment(this.formData)
            this.$message.success('修改部门成功')
          } else {
            // 新增模式
            await postDepartment({ ...this.formData, pid: this.currentid })
            this.$message.success('添加部门成功')
          }
          this.$emit('updateDepartment')
          this.close()
        }
      })
    },
    close() {
      this.formData = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
      this.$refs.departForm.resetFields()
      this.$emit('update:showdialog', false)
    },
    async getDepartmentuser() { // 这个是返回列表数据的接口
      this.managerList = await getDepartmentuser() // f返回的数据 能直接给变量吗
    }
  }
}
</script>

<style>

</style>
